home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: Tue, 16 Jan 1996 13:15:18 GMT
- Organization: Netcom
- Message-ID: <30fba2c3.37871680@nntp.ix.netcom.com>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
- NNTP-Posting-Host: ix-dc6-24.ix.netcom.com
- X-NETCOM-Date: Tue Jan 16 5:15:09 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Holger Maier <100336.3326@CompuServe.COM> wrote:
-
- |>Consider
- |>#include <iostream>
- |>int main() {
- |> int i=1;int j=i+(i+=1);
- |> cout<<i<<','<<j<<'\n';
- |> return 0;
- |>}
- |>on my compiler this produces 2,4
- |>Looked up the ARM:
- |>5: .. The order of evaluation of subexpressions is determined by the
- |>precedence and grouping of operators.
- |>5.7: The additive operators + and - group left to right.
- |>So, j should be assigned 3 ??
- |>Now the question to you C++ gurus out there on the nets:
- |>Is it really a compiler bug or is it just me misinterpreting the
- |>ARM?
-
- You're misinterpretting the ARM. The exact quote from my copy is
-
- The order of evaluation of subexpressions is determined by the
-
- expression grammar. The usual mathematical rules for
- associativity or operators may be applied only where the
- operators really are associative and commutabtive. Except
- where noted, the order of evaluation of operands of individual
-
- operators is undefined.
-
- In the expression A + B, A and B must be evaluated before the sum, but
- there is no requirement as to the order in which they are evaluated.
-
- Note that the draft changes this. Not only is the order of evaluation
- undefined, but in situations like you show in which a variable is
- modified and it's value used, other than to determine the new value,
- the behavior is undefined. The compiler need not produce code that
- evaluates the expression at all -- it could do anything.
-
-
- Michael M Rubenstein
-